public class test7 {
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
        List<List<Integer>> res;
        List<Integer> paths;
        public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
            res = new ArrayList<>();
            paths = new ArrayList<>();
            getPaths(root,targetSum);
            return res;
        }
        private void getPaths(TreeNode root,int targetSum){
            if(root==null){
                return;
            }
            targetSum -= root.val;
            paths.add(root.val);
            if(root.left==null && root.right==null){
                if(targetSum==0){
                    res.add(new ArrayList<>(paths));
                }
            }
            if(root.left!=null){
                getPaths(root.left,targetSum);
                paths.remove(paths.size()-1);
            }
            if(root.right!=null){
                getPaths(root.right,targetSum);
                paths.remove(paths.size()-1);
            }
        }
}
